//
//  UIFont+DefaultFont.m
//  TravelGuideMdd
//
//  Created by xiaofeng on 14-3-11.
//  Copyright (c) 2014年 mafengwo.com. All rights reserved.
//

#import "UIFont+DefaultFont.h"
#import "Utils_Swizzing.h"

@implementation UIFont (DefaultFont)

//static UIFont* (*oldFontWithName_Size_Traits)(id self, SEL cmd, NSString *name, CGFloat size, int traits);
//static UIFont* newFontWithName_Size_Traits(id self, SEL cmd, NSString *name, CGFloat size, int traits)
//{
//    name = [UIFont filterFontName:name];
//    
//    return oldFontWithName_Size_Traits(self, cmd, name, size, traits);
//}

static UIFont* (*oldSystemFontOfSize)(id self, SEL cmd, CGFloat size);
static UIFont* newSystemFontOfSize(id self, SEL cmd, CGFloat size)
{
    UIFont *appfont = [UIFont fontWithName:@"FZLanTingHeiS-L-GB" size:size];
    return appfont?appfont:objc_runOldMethod([UIFont class], oldSystemFontOfSize, self, cmd, size);
}

static UIFont* (*oldBoldSystemFontOfSize)(id self, SEL cmd, CGFloat size);
static UIFont* newBoldSystemFontOfSize(id self, SEL cmd, CGFloat size)
{
    UIFont *appfont = [UIFont fontWithName:@"FZLanTingHeiS-L-GB" size:size];
    return appfont?appfont:objc_runOldMethod([UIFont class], oldBoldSystemFontOfSize, self, cmd, size);
}


+ (NSString*)filterFontName:(NSString*)oldName
{
    NSString *newName = oldName;
    
    if ([oldName rangeOfString:@".HelveticaNeueInterface"].location != NSNotFound)
    {
        newName = @"FZLanTingHeiS-L-GB";
    }
    return newName;
}

+ (void)load
{
    oldSystemFontOfSize = objc_changeClassMethod(self, @selector(systemFontOfSize:), newSystemFontOfSize);
    oldBoldSystemFontOfSize = objc_changeClassMethod(self, @selector(boldSystemFontOfSize:), newBoldSystemFontOfSize);
    
    //    oldFontWithName_Size_Traits = objc_changeClassMethod(self, @selector(fontWithName:size:traits:), newFontWithName_Size_Traits);
    
}



@end


@implementation UILabel (DefaultFont)
static id (*UILabel_oldInitWithCoder)(id self, SEL cmd, NSCoder* coder);
static id UILabel_newInitWithCoder(id self, SEL cmd, NSCoder* coder)
{
    id instance = objc_runOldMethod([UILabel class], UILabel_oldInitWithCoder, self, cmd, coder);
    
    UIFont *oldFont = [instance font];
    if (oldFont)
    {
        [(UILabel*)instance setFont:[UIFont systemFontOfSize:oldFont.pointSize]];
    }
    return instance;
}

static void (*UILabel_oldAwakeFromNib)(id self, SEL cmd);
static void UILabel_newAwakeFromNib(id self, SEL cmd)
{
    objc_runOldMethod([UILabel class], UILabel_oldAwakeFromNib, self, cmd);
    
    UIFont *oldFont = [(UILabel*)self font];
    if (oldFont)
    {
        [(UILabel*)self setFont:[UIFont systemFontOfSize:oldFont.pointSize]];
    }
}

+ (void)load
{
    UILabel_oldInitWithCoder = objc_changeMethod(self, @selector(initWithCoder:), UILabel_newInitWithCoder);
    UILabel_oldAwakeFromNib = objc_changeMethod(self, @selector(awakeFromNib), UILabel_newAwakeFromNib);
}

@end

@implementation UITextField (DefaultFont)
static id (*UITextField_oldInitWithCoder)(id self, SEL cmd, NSCoder* coder);
static id UITextField_newInitWithCoder(id self, SEL cmd, NSCoder* coder)
{
    id instance = objc_runOldMethod([UITextField class], UITextField_oldInitWithCoder, self, cmd, coder);
    
    
    UIFont *oldFont = [instance font];
    if (oldFont)
    {
        [(UITextField*)instance setFont:[UIFont systemFontOfSize:oldFont.pointSize]];
    }
    return instance;
}

static void (*UITextField_oldAwakeFromNib)(id self, SEL cmd);
static void UITextField_newAwakeFromNib(id self, SEL cmd)
{
    objc_runOldMethod([UITextField class], UITextField_oldAwakeFromNib, self, cmd)
    
    UIFont *oldFont = [(UITextField*)self font];
    if (oldFont)
    {
        [(UITextField*)self setFont:[UIFont systemFontOfSize:oldFont.pointSize]];
    }
}

+ (void)load
{
    UITextField_oldInitWithCoder = objc_changeMethod(self, @selector(initWithCoder:), UITextField_newInitWithCoder);
    UITextField_oldAwakeFromNib = objc_changeMethod(self, @selector(awakeFromNib), UITextField_newAwakeFromNib);
}

@end

@implementation UITextView (DefaultFont)
static id (*UITextView_oldInitWithCoder)(id self, SEL cmd, NSCoder* coder);
static id UITextView_newInitWithCoder(id self, SEL cmd, NSCoder* coder)
{
    id instance = objc_runOldMethod([UITextView class], UITextView_oldInitWithCoder, self, cmd, coder);
    
    UIFont *oldFont = [instance font];
    if (oldFont)
    {
        [(UITextView*)instance setFont:[UIFont systemFontOfSize:oldFont.pointSize]];
    }
    return instance;
}

static void (*UITextView_oldAwakeFromNib)(id self, SEL cmd);
static void UITextView_newAwakeFromNib(id self, SEL cmd)
{
    objc_runOldMethod([UITextView class], UITextView_oldAwakeFromNib, self, cmd);
    
    UIFont *oldFont = [(UITextView*)self font];
    if (oldFont)
    {
        [(UITextView*)self setFont:[UIFont systemFontOfSize:oldFont.pointSize]];
    } else {
        [(UITextView*)self setFont:[UIFont systemFontOfSize:14]];
    }
}

+ (void)load
{
    UITextView_oldInitWithCoder = objc_changeMethod(self, @selector(initWithCoder:), UITextView_newInitWithCoder);
    UITextView_oldAwakeFromNib = objc_changeMethod(self, @selector(awakeFromNib), UITextView_newAwakeFromNib);
}

@end


@implementation UIButton (DefaultFont)
static id (*UIButton_oldInitWithCoder)(id self, SEL cmd, NSCoder* coder);
static id UIButton_newInitWithCoder(id self, SEL cmd, NSCoder* coder)
{
    id instance = objc_runOldMethod([UIButton class], UIButton_oldInitWithCoder, self, cmd, coder);
    
    UILabel *label = [(UIButton*)instance titleLabel];
    UIFont *oldFont = [label font];
    if (oldFont)
    {
        [label setFont:[UIFont systemFontOfSize:oldFont.pointSize]];
    }
    return instance;
}

static void (*UIButton_oldAwakeFromNib)(id self, SEL cmd);
static void UIButton_newAwakeFromNib(id self, SEL cmd)
{
    objc_runOldMethod([UIButton class], UIButton_oldAwakeFromNib, self, cmd);
    
    UILabel *label = [(UIButton*)self titleLabel];
    UIFont *oldFont = [label font];
    if (oldFont)
    {
        [label setFont:[UIFont systemFontOfSize:oldFont.pointSize]];
    }
}

+ (void)load
{
    UIButton_oldInitWithCoder = objc_changeMethod(self, @selector(initWithCoder:), UIButton_newInitWithCoder);
    UIButton_oldAwakeFromNib = objc_changeMethod(self, @selector(awakeFromNib), UIButton_newAwakeFromNib);
}

@end
